;(function($) {
	var alarmDataManager = function(ele, opt) {
		this.$element = ele;
		this.defaults = {
				dataGuids : null,
				videoGuid:null,
				leftChannel:"leftDiv",
				rightChannel:"rightDiv",
				realTimeAlarmDiv :"realTimeDiv",//实时报警数据
				hourAlarmDiv :"hourAlarmDiv",//小时报警
				codeList:"codeList",//采样污染物
				dataShow:"dataShow"//采样曲线
		};
		this.options = $.extend({}, this.defaults, opt);
	}
	alarmDataManager.prototype ={
			init : function(){
				this.$element.show();
				var width  = this.$element.width();
				//仅一路数采仪
				//tip: 先行展示小时数据，通过小时数据中的超标数据可以查看对应时段内的实时报警数据和对应的数据曲线图
				if(this.options.dataGuids!=null && this.options.dataGuids!=undefined && this.options.dataGuids.length>0){
					console.log("length:"+this.options.dataGuids.length);
					if(this.options.dataGuids.length == 1){
						this.$element.find("#left-tab").children().eq(0).css({width:width/2 -10});
						this.$element.find("#left-tab").show();
						var tab = "left";
						//this.getRealTimeAlarmData(this.options.dataGuids[0], tab);
						this.getHourAlarmData(this.options.dataGuids[0], tab);
						//this.getCodeList(this.options.dataGuids[0], tab);
					}else if(this.options.dataGuids.length == 2){//双路数采仪
						this.$element.find("#left-tab").children().eq(0).css({width:width/2-10});
						this.$element.find("#right-tab").children().eq(0).css({width:width/2-10});
						this.$element.find("#left-tab").show();
						this.$element.find("#right-tab").show();
						var tab1 = "left";
						//this.getRealTimeAlarmData(this.options.dataGuids[0], tab1);
						this.getHourAlarmData(this.options.dataGuids[1], tab1);
						//this.getCodeList(this.options.dataGuids[0], tab1);
						var tab2 = "right";
						//this.getRealTimeAlarmData(this.options.dataGuids[1], tab2);
						this.getHourAlarmData(this.options.dataGuids[0], tab2);
						//this.getCodeList(this.options.dataGuids[1], tab2);
					}else{
						this.$element.hide();
					}
				}
				if(this.options.videoGuid!=null && this.options.videoGuid != undefined &&this.options.videoGuid.length>0){
					this.getRealTimeVideoAlarmData(this.options.videoGuid[0].guid,this.options.videoGuid[0].name,"videoAlarm");
				}
				return this;
			},
			//传入视频GUID ，数据通道 target:jquery对象
			getRealTimeVideoAlarmData:function(guid,name,target){
				//获取实时视频报警
				var options = {
					target : $.CurrentNavtab.find("#"+target),
					url : "loadRealTimeVideoAlarm.shtml",
					type : 'get',
					data : {
						"guid" : guid,
						"name":name.replaceAll('#','%23')
					},
					loadingmask : true,
					autorefresh : 600 //60秒刷新一次
				};
				this.$element.bjuiajax('doLoad', options);
			},
			getRealTimeAlarmData:function(guid,tab,hour,code){
				//获取对应小时内的实时数据报警
				var options = {
					target : $.CurrentNavtab.find("#realTimeAlarmDiv-"+tab),
					url : "loadRealTimeAlarm.shtml",
					type : 'get',
					data : {
						"guid" : guid,
						"hour" :hour,
						"code":code
					},
					loadingmask : true,
					autorefresh : 3600 //没个60秒刷新一次
				};
				this.$element.bjuiajax('doLoad', options);
			},
			getHourAlarmData:function(guid,tab){
				//获取小时报警
				var options = {
						target : $.CurrentNavtab.find("#hourAlarmDiv-"+tab),
						url : "loadHourAlarm.shtml",
						type : 'get',
						data : {
							"guid" : guid
						},
						loadingmask : true,
						autorefresh : 1800 //每隔30分钟刷新一次
					};
				this.$element.bjuiajax('doLoad', options);
			},
			getCodeList:function(dataGuid,tab){
				var $this = this;
				//获取污染物列表
				$
				.ajax({
					url : 'loadCodeList.shtml?guid=' + dataGuid+ "&specialId=" + (new Date().getTime()),
					dataType : 'json',
					type : "post",
					statusCode : {
						300 : function() {
							console.log("返回状态码为：300");
						}
					},
					success : function(data, textStatus, jqXHR) {
						if (data.statusCode == "200") {
							var list = data.bean;
							console.log(list)
							var inputs = "";
							$this.$element.find("#codeList-"+tab).html("");
							for (var i = 0;list!=null &&  i < list.length; i++) {
								console.log(list[i].code + ":" + list[i].codename)
								var input = '<input id="cod_'
										+ list[i].code + tab
										+ '" type="checkbox" data-toggle="icheck" data-label="'
										+ list[i].codename + '" data-group="'+tab+'"  value="'
										+ list[i].code + '"/>'
										+ '<label class="ilabel" for="cod_'
										+ list[i].code +tab + '">' + list[i].codename
										+ '</label>';
								$this.$element.find("#codeList-"+tab).append(input);
							}
							$this.$element.find("input[type=checkbox]").iCheck({
								checkboxClass : 'icheckbox_minimal-purple',
								radioClass : 'iradio_minimal-purple',
								increaseArea : '20%' // optional
							});
							$this.$element.find("input[type=checkbox]").off("ifChecked").off("ifUnchecked");
							$this.$element.find("input[type=checkbox]")
									.on(
											"ifChecked",
											function(event) {
												// 获取该污染物采样数据
												var selCode = event.target
														.getAttribute("value");
												var group = event.target.getAttribute("data-group");
												var $checkElement= $this.$element.find("#" + selCode+"-"+group);
												if ($checkElement.length > 0 && $checkElement.html()!="") {
													$checkElement.show();
												} else {
													$this.createShowDiv(selCode,group);
													$this.getCodeLineRecords(dataGuid,
															selCode,group);
												}
											});
							$this.$element.find("input[type=checkbox]")
							.on(
									"ifUnchecked",
									function(event) {
										var selCode = event.target
												.getAttribute("value");
										var group = event.target.getAttribute("data-group");
										var $checkElement= $this.$element.find("#" + selCode+"-"+group);
										if ($checkElement.length > 0) {
											$checkElement.hide();
										}
									});
						}
					},
					error : function() {
						$(document).alertmsg('warn', '当前监测点IVS污染物采样列表获取超时！', {
							displayMode : 'slide',
							displayPosition : 'topcenter',
							okName : '好的',
							title : '提示'
						});
					},
					timeout : 5000
				});
			},
			getCodeLineRecords:function(guid,code,tab){
				var $this = this;
				//获取对应污染物采样记录
				$.ajax({
					url : 'loadCodeSamplingRecords.shtml?guid=' + guid + "&code=" + code
							+ "&specialId=" + (new Date().getTime()),
					dataType : 'json',
					statusCode : {
						300 : function() {
							console.log("返回状态码为：300");
						}
					},
					success : function(data, textStatus, jqXHR) {
						console.log(data.statusCode)
						var dataStr = data.message;
						console.log(dataStr);
						var value = 0;
						var title = data.bean.name;
						var label = data.bean.name;
						var unit = data.bean.unit;
						$this.arearange(dataStr, code+"-"+tab, value, title, label, unit);
					},
					error : function() {
					}
				});
			},
			arearange:function(data, divID, value, title, label, unit) {
				var $this = this;
				/*var data = [[1457060877000,86],[1457060882000,84],[1457060887000,83],[1457060892000,81],[1457060897000,95],[1457060902000,94],[1457060907000,92],[1457060912000,91],[1457060917000,89],[1457060922000,24],[1457060927000,23],[1457060932000,21],[1457060937000,98],[1457060942000,97],[1457060947000,32],[1457060952000,30],[1457060958000,29],[1457060963000,27],[1457060968000,26],[1457060973000,40],[1457060978000,38],[1457060983000,37],[1457060988000,35],[1457060993000,33],[1457060998000,47],[1457061003000,46],[1457061008000,44],[1457061013000,43],[1457061018000,41],[1457061023000,40],[1457061028000,54],[1457061033000,52],[1457061038000,50],[1457061043000,49],[1457061049000,47],[1457061054000,61],[1457061059000,60],[1457061064000,58],[1457061069000,57],[1457061074000,55],[1457061079000,54],[1457061084000,68],[1457061089000,66],[1457061094000,64],[1457061099000,63],[1457061104000,61],[1457061109000,75],[1457061114000,74],[1457061119000,72],[1457061124000,71]];
				var divID = 'S02';
				var title = "S02";
				var value = "50";
				var label = "烟气流速";
				var unit = "m3/h";*/
				console.log(data + ":" + divID + ":" + value + ":" + title + ":" + label
						+ ":" + unit);
				// 时实数据
				Highcharts.setOptions({
					global : {
						useUTC : false
					},
					lang : {
						resetZoom : '恢复初始'
					}
				});
				var hightcharDiv ;
				if($.CurrentDialog && $.CurrentDialog.find('#' + divID))
					hightcharDiv = $.CurrentDialog.find('#' + divID);
				else
					hightcharDiv = $this.$element.find('#' + divID);
				hightcharDiv.highcharts(
						{
							chart : {
								type : 'line',
								zoomType : 'x'
							},
							lang : {
								noData : "当前没有采样数据"
							},
							title : {
								text : title
							},
							xAxis : {
								type : 'datetime',
								dateTimeLabelFormats : {
									second : '%H:%M:%S',
									minute : '%H:%M',
									hour : '%H:%M',
									day : '%Y-%m-%e',
									month : '%Y-%m',
									year : '%Y'
								}
							},
							yAxis : {
								title : {
									text : null
								},
								plotLines : [ {
									color : 'red',
									dashStyle : 'solid',
									value : value,
									width : 0.5
								} ]
							},
							tooltip : {
								formatter : function() {
									return '<b>'
											+ this.series.name
											+ '</b><br/>'
											+ Highcharts.dateFormat('%Y年%m月%e日 %H:%M:%S',
													this.x) + '： ' + this.y + "  " + unit;
								}
							},
							legend : {
								enabled : false
							},
							series : [ {
								name : label,
								data : eval(data)
							} ]
						});
			},
			createShowDiv : function(divId,tab) {
				var $this = this;
				if($this.$element.find("#"+divId+"-"+tab).length>0)
					return;
				var div = '<div class="col-md-12" id="' + divId+"-"+tab
						+ '" style="mini-width: 400px; height: 350px"></div>';
				$this.$element.find("#dataShow-"+tab).append(div);
			}
	};
	$.fn.alarmDataManager = function(opt) {
		var alarmDM = new alarmDataManager(this, opt);
		alarmDM.init();
		return alarmDM;
	}
})(jQuery);

/***
 * WinAP GUID播放
 */
function openLiveA(){
	var liveGuids = $.CurrentNavtab.find("#videoGuid").data("videoGuid");
	console.log(liveGuids.length);
	var $player  =  $.CurrentNavtab.find("#videoObject")[0];
	for(var i=0;i<liveGuids.length;i++){
		var ret = $player.MMSWP_OpenLive(liveGuids[i], 2, 1);
		console.log(liveGuids[i]+"  -- result:"+ret);
		if (0 == ret)
			$(document).alertmsg('warn', '视频播放失败，请联系管理员确认前端视频设备连接正常！', {
				displayMode : 'slide',
				displayPosition : 'topcenter',
				okName : '好的',
				title : '错误'
			});
		else{
			$.CurrentNavtab.find("#stopBtn").removeAttr("disabled");
			$.CurrentNavtab.find("#playBtn").attr("disabled","disabled");
		}
	}
	return liveGuids;
}
function closeLiveA(){
	var liveGuids = $.CurrentNavtab.find("#videoGuid").data("videoGuid");
	console.log(liveGuids.length);
	for(var i=0;i<liveGuids.length;i++){
		$.CurrentNavtab.find("#videoObject")[0].MMSWP_CloseLive(liveGuids[i]);
	}
	$.CurrentNavtab.find("#playBtn").removeAttr("disabled");
	$.CurrentNavtab.find("#stopBtn").attr("disabled","disabled");
}
/***
 * RTSP协议方式播放
 */
function openLive() {
	var ret = $.CurrentNavtab.find("#videoObject")[0].OpenRtspUrl(
			"rtsp://admin:xjhbt@120.205.11.86:8088/video_channel=0_stream=0");
	if (0 == ret)
		$(document).alertmsg('warn', '视频播放失败，请联系管理员确认前端视频设备连接正常！', {
			displayMode : 'slide',
			displayPosition : 'topcenter',
			okName : '好的',
			title : '错误'
		});
	else{
		$.CurrentNavtab.find("#stopBtn").removeAttr("disabled");
		$.CurrentNavtab.find("#playBtn").attr("disabled","disabled");
	}
}

function closeLive() {
	var ret = $.CurrentNavtab.find("#videoObject")[0].CloseRtspUrl();
	$.CurrentNavtab.find("#playBtn").removeAttr("disabled");
	$.CurrentNavtab.find("#stopBtn").attr("disabled","disabled");
}
function fullScreen() {
	var status = $.CurrentNavtab.find(".bjui-pageContent").data(
	"playStatus");

	if(status.videoWinType==1){
		//winAp
		$.CurrentNavtab.find("#videoObject")[0].MMSWP_FullScreen("");
	}else if(status.videoWinType==2){
		$.CurrentNavtab.find("#videoObject2")[0].SMS_FullScreen();
	}
	
}

/***
 * 文件下载
 * @param a
 */
function filedownload(href) {
	$.fileDownload(href, {
		failCallback : function(responseHtml, url) {
			if (responseHtml.trim().startsWith('{'))
				responseHtml = responseHtml.toObj()
			$(document.body).bjuiajax('ajaxDone', responseHtml)
		}
	})
}
